#include "systemfile.h"
#include <QString>
#include <stdio.h>
#include <QDataStream>
#include <QtGui>
#include <QDebug>
#include <QMap>
systemFile::systemFile()
{
    QString fileName=QString("%1/settings.ini").arg(QDir::currentPath());
    this->devSetting = new QSettings(fileName,QSettings::IniFormat);
    if(QFile::exists(fileName) == false)
    {
        this->resetSetting();
        if(!this->devSetting->contains("DEVICE"))
        {
            qDebug()<<"devSetting no DEVICE";
        }else  if(!this->devSetting->contains("SERVER"))
        {
            qDebug()<<"devSetting no SERVER";
        }else if(!this->devSetting->contains("CAMERA"))
        {
            qDebug()<<"devSetting no CAMERA";
        }else if(!this->devSetting->contains("MODEL"))
        {
            qDebug()<<"devSetting no MODEL";
        }
    }    
    //读取MAC地址
    QNetworkInterface  localInterface =localInterface.interfaceFromName("eth0");
    this->mac =localInterface.hardwareAddress();
    qDebug() <<"mac address:"<< this->mac;
    this->readInterfaces();
    this->readSetting();

}
//读取系统设置
bool systemFile::readSetting()
{
    //
    this->devSetting->setIniCodec(QTextCodec::codecForName("UTF-8"));
    this->devSetting->beginGroup("DEVICE");
    this->galleryName = this->devSetting->value("galleryName").toString();
   //this->galleryName = "智能停车场";
    this->galleryNumber = this->devSetting->value("galleryNumber").toString();
    this->regNumber = this->devSetting->value("regNumber").toString();
    this->heart = this->devSetting->value("heart").toInt();
    this->path = this->devSetting->value("path").toString();
    this->car_stall=this->devSetting->value("stall").toInt();

    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("SERVER");
    this->serverIp = this->devSetting->value("serverIp").toString();
    this->serverUrl = this->devSetting->value("serverUrl").toString();
    this->updataPicture = this->devSetting->value("updataPicture").toInt();
    this->updataTime = this->devSetting->value("updataTime").toTime();
    this->downTime = this->devSetting->value("downTime").toString();
    this->userName = this->devSetting->value("userName").toString();
    this->passWord = this->devSetting->value("passWord").toString();
    this->datebaseName = this->devSetting->value("datebaseName").toString();
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("CAMERA");
    this->cameraInIp = this->devSetting->value("cameraInIp").toString();
    this->cameraOutIp = this->devSetting->value("cameraOutIp").toString();
    this->savePicture = this->devSetting->value("savePicture").toInt();
    this->vehicleFlag = this->devSetting->value("vehicleFlag").toInt();
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("MODEL");
    this->model = this->devSetting->value("model").toInt();
    this->tempCarIn = this->devSetting->value("tempCarIn").toInt();
    this->passFlag = this->devSetting->value("passFlag").toInt();
    this->first_down = this->devSetting->value("first_down").toInt();
    this->park_type = this->devSetting->value("park_type").toInt();
    this->park_camera = this->devSetting->value("park_camera").toInt();
    if(QString::number(this->park_camera).isEmpty())
    {
    this->park_camera = 2;
    }
    this->devSetting->endGroup();


    this->devSetting->beginGroup("SPARE");
    this->cameraInIpBei = this->devSetting->value("cameraInIpBei").toString();
    this->cameraOutIpBei = this->devSetting->value("cameraOutIpBei").toString();
    this->mysqlServerIp = this->devSetting->value("mysqlServerIp").toString();
    this->serverIpBei = this->devSetting->value("serverIpBei").toString();
    this->ledIp1 = this->devSetting->value("ledIp1").toString();
    this->ledIp2 = this->devSetting->value("ledIp2").toString();
    this->sqdbPath = this->devSetting->value("sqdbPath").toString();
    this->devSetting->endGroup();
    qDebug()<<"****************************************";
    qDebug()<<"******DEVICE********";
    qDebug()<<"galleryName**"<<this->galleryName;
    qDebug()<<"galleryNumber"<<this->galleryNumber;
    qDebug()<<"regNumber****"<<this->regNumber;
    qDebug()<<"heart********"<<this->heart;
    qDebug()<<"path*********"<<this->path;
    qDebug()<<"passFlag*****"<<this->passFlag;
    qDebug()<<"stall*****"<<this->car_stall;

    qDebug()<<"******SERVER********";
    qDebug()<<"serverIp*****"<<this->serverIp;
    qDebug()<<"serverUrl****"<<this->serverUrl;
    qDebug()<<"updataPicture"<<this->updataPicture;
    qDebug()<<"updataTime***"<<this->updataTime;
    qDebug()<<"downTime*****"<<this->downTime;
    qDebug()<<"userName*****"<<this->userName;
    qDebug()<<"passWord*****"<<this->passWord;
    qDebug()<<"datebaseName*"<<this->datebaseName;
    qDebug()<<"******CAMERA********";
    qDebug()<<"cameraInIp*****"<<this->cameraInIp;
    qDebug()<<"cameraOutIp****"<<this->cameraOutIp;
    qDebug()<<"savePicture****"<<this->savePicture;
    qDebug()<<"vehicleFlag****"<<this->vehicleFlag;
    qDebug()<<"******MODEL********";
    qDebug()<<"model********"<<this->model;
    qDebug()<<"tempCarIn****"<<this->tempCarIn;
    qDebug()<<"passFlag*****"<<this->passFlag;
    qDebug()<<"first_down****"<<this->first_down;
    qDebug()<<"park_type****"<<this->park_type;
    qDebug()<<"park_camera****"<<this->park_camera;
    qDebug()<<"******SPARE*******";
    qDebug()<<"cameraInIpBei*****"<<this->cameraInIpBei;
    qDebug()<<"cameraOutIpBei****"<<this->cameraOutIpBei;
    qDebug()<<"mysqlServerIp*****"<<this->mysqlServerIp;
    qDebug()<<"serverIpBei****"<<this->serverIpBei;
    qDebug()<<"ledIp1****"<<this->ledIp1;
    qDebug()<<"ledIp2****"<<this->ledIp2;
    qDebug()<<"sqdbPath****"<<this->sqdbPath;
    qDebug()<<"****************************************";
    return true;
}
//读取网络设置
bool systemFile::readInterfaces()
{
    QFile interfaces("/etc/network/interfaces");
    QByteArray buffer;
    QString str;
    QStringList strList;
    QMap<QString,QString> map;
    //red interfaces
    interfaces.open(QIODevice::ReadOnly);
     while(1)
    {
        buffer = interfaces.readLine();
        if(buffer.length() == 0)
        {
            break;
        }
        str = QString(buffer);
        strList = str.split(" ");
        if(strList.count() >=2)
        {
            map.insert(strList[0],strList[1]);
        }
    }
    interfaces.close();
    this->localIp = map["address"].trimmed();
    this->gatewayIp = map["gateway"].trimmed();
    this->mask = map["netmask"].trimmed();
    qDebug() << "Ip:"<< this->localIp;
    qDebug() << "gateway:"<< this->gatewayIp;
    qDebug() << "mask:" << this->mask;
    return true;
}

bool systemFile::resetSetting()
{
    QString fileName=QString("%1/settings.ini").arg(QDir::currentPath());
    QString fileInfo;
    QFile myfile(fileName);
    qDebug() << "*********************************";
    qDebug() << "reset setting";
    qDebug() << "*********************************";
    myfile.open(QIODevice::WriteOnly);
    //
    fileInfo.append("[DEVICE]\r\n");
    fileInfo.append("galleryName =智能停车场\r\n");
    fileInfo.append("galleryNumber = 1\r\n");
    fileInfo.append("regNumber = 00000000\r\n");
    fileInfo.append("heart = 40\r\n");
    fileInfo.append("path = /home/root/\r\n");
    fileInfo.append("stall = 1\r\n");

    //
    fileInfo.append("[SERVER]\r\n");
    fileInfo.append("serverIp = 192.168.10.200\r\n");
    fileInfo.append("serverUrl = test\r\n");
    fileInfo.append("updataPicture = 1\r\n");
    fileInfo.append("updataTime = 01:00:00\r\n");
    fileInfo.append("downTime = 30\r\n");
    fileInfo.append("userName = copy\r\n");
    fileInfo.append("passWord = 123\r\n");
    fileInfo.append("datebaseName = pn\r\n");
    //
    fileInfo.append("[CAMERA]\r\n");
    fileInfo.append("cameraInIp = 192.168.10.223\r\n");
    fileInfo.append("cameraOutIp = 192.168.10.224\r\n");
    fileInfo.append("savePicture = 0\r\n");
    fileInfo.append("vehicleFlag = 0\r\n");
    //
    fileInfo.append("[MODEL]\r\n");
    fileInfo.append("model = 3\r\n");
    fileInfo.append("tempCarIn = 0\r\n");
    fileInfo.append("passFlag = 0\r\n");
    fileInfo.append("first_down = 1\r\n");
    fileInfo.append("park_type = 1\r\n");
    fileInfo.append("park_camera = 1\r\n");
    //
    fileInfo.append("[SPARE]\r\n");
    fileInfo.append("cameraInIpBei = 127.0.0.1\r\n");
    fileInfo.append("cameraOutIpBei = 127.0.0.1\r\n");
    fileInfo.append("mysqlServerIp = 127.0.0.1\r\n");
    fileInfo.append("serverIpBei = 127.0.0.1\r\n");
    fileInfo.append("ledIp1 = 127.0.0.1\r\n");
    fileInfo.append("ledIp2 = 127.0.0.1\r\n");
    fileInfo.append("sqdbPath = /home/root/\r\n");
    //

    myfile.write(fileInfo.toLocal8Bit());
    myfile.close();
    return true;
}
/*bool systemFile::writeInterfaces()
{
    QByteArray buffer;
    QString str;
    QFile interfaces("/etc/network/interfaces");
    //interfaces
    interfaces.open(QIODevice::WriteOnly);
    //head
    str = QString("auto eth0\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface eth0 inet static\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    //ip
    str = QString("address %1").arg(this->localIp);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\r\n");
    //gateway
    str = QString("gateway %1").arg(this->gatewayIp);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\r\n");
    //mask
    str = QString("netmask %1").arg(this->mask);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\r\n");

    //broadcast
    str = QString("broadcast 255.255.255.255");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\r\n");
    interfaces.close();
    return true;
}*/

bool systemFile::writeInterfaces()
{
    QByteArray buffer;
    QString str;
    QFile interfaces("/etc/network/interfaces");
    //interfaces
    interfaces.open(QIODevice::WriteOnly);
    str = QString("auto lo\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("iface lo inet loopback\n");
    buffer = str.toLocal8Bit();

    interfaces.write(buffer);
    str = QString("iface wlan0 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wireless_mode managed\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wireless_essid any\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wpa-driver wext\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wpa-conf /etc/wpa_supplicant.conf\n\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface tiwlan0 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wireless_mode managed\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    str = QString("wireless_essid any\n\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface atml0 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    //head
    str = QString("auto eth0\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface eth0 inet static\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    //ip
    str = QString("address %1").arg(this->localIp);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\n");
    //gateway
    str = QString("gateway %1").arg(this->gatewayIp);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\n");
    //mask
    str = QString("netmask %1").arg(this->mask);
    str.trimmed();
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\n");

    //broadcast
    str = QString("broadcast 255.255.255.255");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);
    interfaces.write("\n");

    str = QString("pre-up /bin/grep -v -e \"ip=[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\" /proc/cmdline > /dev/null\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface eth1 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface usb0 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    str = QString("iface bnep0 inet dhcp\n");
    buffer = str.toLocal8Bit();
    interfaces.write(buffer);

    interfaces.close();
    return true;
}

